package com.zr.task;

import android.Manifest;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.RequiresPermission;
import androidx.appcompat.app.AppCompatActivity;

import com.zh.baselibrary.netutils.NetUtil;
import com.zh.baselibrary.utils.BarUtil;
import com.zh.baselibrary.utils.IntentUtils;
import com.zh.baselibrary.utils.LogUtils;
import com.zr.task.activity.WelcomeActivity;


public class SplashActivity extends AppCompatActivity {

    NetworkChangedReceiver networkChangedReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_splash);
        BarUtil.closeBar();

        if (NetUtil.checkNetwork(this) == NetUtil.NO_NETWORK) {

            networkChangedReceiver = new NetworkChangedReceiver();
            IntentFilter filter = new IntentFilter();
            filter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
            filter.addAction("android.net.wifi.WIFI_STATE_CHANGED");
            filter.addAction("android.net.wifi.STATE_CHANGE");
            registerReceiver(networkChangedReceiver, filter);
        } else {
            startWelcomeActivity();
        }


        findViewById(R.id.tv_net_error).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                threeClick();
            }
        });
    }

    private int count;
    private Long start, end;

    private void threeClick() {
        count++;
        if (count == 1) {
            start = System.currentTimeMillis();
        }
        if (count == 3) {
            end = System.currentTimeMillis();
        }
        if (count >= 3) {
            if ((end - start) < 700) {
                BarUtil.showBar();
                finish();
                Intent intent = new Intent();// 为Intent设置Action、Category属性
                intent.setAction(Intent.ACTION_MAIN);// "android.intent.action.MAIN"
                intent.addCategory(Intent.CATEGORY_HOME); //"android.intent.category.HOME"
                startActivity(intent);

            }
            count = 0;
        }
        if ((System.currentTimeMillis() - start) > 1000) {
            count = 0;
            start = System.currentTimeMillis();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (networkChangedReceiver != null) {
            unregisterReceiver(networkChangedReceiver);
        }
    }

    public void startWelcomeActivity() {
        IntentUtils.instance().startActivityFinish(this, WelcomeActivity.class);
    }

    class NetworkChangedReceiver extends BroadcastReceiver {


        public NetworkChangedReceiver() {
        }


        @RequiresPermission(Manifest.permission.ACCESS_NETWORK_STATE)
        @Override
        public void onReceive(Context context, Intent intent) {
            if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())) {
                ConnectivityManager manager = (ConnectivityManager) context
                        .getSystemService(Context.CONNECTIVITY_SERVICE);
                if (manager == null) return;
                NetworkInfo activeNetwork = manager.getActiveNetworkInfo();
                if (activeNetwork != null) {
                    if (activeNetwork.isConnected()) {
                        if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI) {
                            LogUtils.d( "网络连接发生变化，当前WiFi连接可用，正在尝试重连。");
                        } else if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE) {
                            LogUtils.d("网络连接发生变化，当前移动连接可用，正在尝试重连。");
                        }
                        startWelcomeActivity();
                    } else {
                        LogUtils.d("当前没有可用网络");
                    }
                }
            }
        }
    }
}
